package com.rdc.qingkou.model;


import com.rdc.qingkou.config.URL;
import com.rdc.qingkou.dto.CardDTO;
import com.rdc.qingkou.dto.CustomTagDTO;
import com.rdc.qingkou.model.imodel.IHomepageModel;
import com.rdc.qingkou.presenter.HomepagePresenter;
import com.rdc.qingkou.retrofitService.IHomepageService;
import com.rdc.qingkou.utils.MyUtils;
import com.rdc.qingkou.vo.CardVO;

import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Response;


public class HomepageModel implements IHomepageModel {


    private CompositeDisposable mCompositeDisposable = new CompositeDisposable();
    private HomepagePresenter presenter;
    private CustomTagDTO customTagDTO;
    private String errorMessage;

    public HomepageModel(HomepagePresenter presenter) {
        this.presenter = presenter;

    }

    @Override
    public void sendRequest() {
        IHomepageService homepageService = MyUtils.getRetrofitProvider(URL.GET_CUSTOM_TAG).create(IHomepageService.class);
        homepageService.getCustomTags()
                .subscribeOn(Schedulers.io())
                .observeOn(Schedulers.io())
                .flatMap(new Function<Response<CustomTagDTO>, Observable<Response<List<CardDTO>>>>() {
                    @Override
                    public Observable<Response<List<CardDTO>>> apply(@NonNull Response<CustomTagDTO> customTagDTOResponse) throws Exception {
                        if (customTagDTOResponse.code() != HttpURLConnection.HTTP_OK) {
                            errorMessage = "主页获取自定义标签出错！error code:" + customTagDTOResponse.code();
                            return Observable.error(new Throwable("主页获取自定义标签出错！"));
                        }
                        customTagDTO = customTagDTOResponse.body();
                        StringBuilder sb = new StringBuilder();
                        if (!customTagDTO.getResult().equals("")) {
                            for (String tag : customTagDTO.transform()) {
                                sb.append(tag).append(",");
                            }
                        }
                        return MyUtils.getRetrofitProvider(URL.GET_COVER).create(IHomepageService.class).getHomepageCard(sb.toString());
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Response<List<CardDTO>>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onNext(Response<List<CardDTO>> listResponse) {
                        if (listResponse.code() != HttpURLConnection.HTTP_OK) {
                            presenter.RequestError("主页获取卡片失败！error code:" + listResponse.code());
                            return;
                        }
                        List<CardVO> cardVOList = new ArrayList<>();
                        for (CardDTO cardDTO : listResponse.body()) {
                            if (cardDTO != null) {
                                cardVOList.add(cardDTO.transformToCard());
                            }
                        }
                        presenter.RequestSuccess(customTagDTO.transform(), cardVOList);
                    }

                    @Override
                    public void onError(Throwable e) {
                        if (e.getMessage().equals("主页获取自定义标签出错！")) {
                            presenter.RequestError(errorMessage);
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void clearDisposable() {
        mCompositeDisposable.dispose();
    }

    @Override
    public void addDisposable(Disposable disposable) {
        mCompositeDisposable.add(disposable);
    }
}
